package com.sample.tdf.sample.service.impl;

import cn.com.taiji.common.dto.PageDTO;
import cn.com.taiji.common.util.CommonBeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sample.tdf.sample.domain.Course;
import com.sample.tdf.sample.domain.Student;
import com.sample.tdf.sample.dto.CourseDTO;
import com.sample.tdf.sample.dto.StudentDTO;
import com.sample.tdf.sample.mapper.CourseMapper;
import com.sample.tdf.sample.service.ICourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public PageDTO getPage(PageDTO pageDTO) {
        return null;
    }

    @Override
    public CourseDTO create(CourseDTO dto) {
        Course course = new Course();
        CommonBeanUtil.saveCopy(dto, course);
        save(course);
        CommonBeanUtil.saveCopy(course, dto);
        return dto;

    }

    @Override
    public List<CourseDTO> batchCreate(List<CourseDTO> dtos) {
        List<Course> list = new ArrayList();
        dtos.forEach((dto) -> {
            Course course = new Course();
            CommonBeanUtil.saveCopy(dto, course);
            CommonBeanUtil.saveCopy(course, dto);
            list.add(course);
        });
        saveBatch(list);
        return dtos;
    }

    @Override
    public CourseDTO update(CourseDTO dto) {
        Course course = new Course();
        CommonBeanUtil.saveCopy(dto, course);
        updateById(course);
        CommonBeanUtil.saveCopy(course, dto);
        return dto;
    }

    @Override
    public CourseDTO findById(String id) {
        Course byId = courseMapper.findById(id);
        CourseDTO courseDTO = new CourseDTO();
        CommonBeanUtil.saveCopy(byId, courseDTO);
        Student student = byId.getStudent();
        StudentDTO studentDTO = new StudentDTO();
        CommonBeanUtil.saveCopy(student, studentDTO);
        courseDTO.setStudentDTO(studentDTO);
        return courseDTO;
    }

    @Override
    public CourseDTO findByIdOneSelect(String id) {
        Course byIdOneSelect = courseMapper.findByIdOneSelect(id);
        Student student = byIdOneSelect.getStudent();
        CourseDTO courseDTO = new CourseDTO();
        StudentDTO studentDTO = new StudentDTO();
        CommonBeanUtil.saveCopy(student, studentDTO);
        CommonBeanUtil.saveCopy(byIdOneSelect, courseDTO);
        courseDTO.setStudentDTO(studentDTO);
        return courseDTO;
    }
}
